/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.daos.ProductDao;
import com.insurance.cis.models.Product;
import com.insurance.cis.models.ProductCategory;
import com.insurance.cis.scos.ProductSearchCondition;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.views.ProductJDialogCreationView;
import com.insurance.cis.views.ProductJDialogUpdateView;
import com.insurance.cis.views.ProductMasterJPanelView;
import javax.swing.JDialog;
import javax.swing.JPanel;

public class ProductController {

    ProductMasterJPanelView mainJpnl;
    ProductJDialogCreationView creationView;
    ProductJDialogUpdateView updateView;
    ProductDao productDao = new ProductDao();

    public ProductController(JPanel jPanel) {
        mainJpnl = (ProductMasterJPanelView) jPanel;
    }

    public ProductController(JDialog jDialog) {
        if (jDialog instanceof ProductJDialogCreationView) {
            creationView = (ProductJDialogCreationView) jDialog;
        } else if (jDialog instanceof ProductJDialogUpdateView) {
            updateView = (ProductJDialogUpdateView) jDialog;
        }
    }

    public ProductSearchCondition validateSearchConditions() {
        String productID = "";
        String productName = "";
        Integer productCatID = null;
        String productDesc = "";
        if (mainJpnl != null) {
            productID = mainJpnl.getTxtProductID().getText().trim();
            productName = mainJpnl.getTxtProductName().getText().trim();
            productCatID = ((ProductCategory) mainJpnl.getCbxProductCat().getSelectedItem()).getId();
            productDesc = mainJpnl.getTxtProductDesc().getText().trim();
        }
        if (creationView != null) {
            productID = creationView.getTxtProductID().getText().trim();
            productName = creationView.getTxtProductName().getText().trim();
            productCatID = ((ProductCategory) creationView.getCbxCategories().getSelectedItem()).getId();
            productDesc = creationView.getTxaDescription().getText().trim();
        } else if (updateView != null) {
            productID = updateView.getTxtProductID().getText().trim();
            productName = updateView.getTxtProductName().getText().trim();
            productCatID = ((ProductCategory) updateView.getCbxCategories().getSelectedItem()).getId();
            productDesc = updateView.getTxaDescription().getText().trim();
        }
        ProductSearchCondition sco = new ProductSearchCondition(
                productID, productName, productCatID);
        sco.setProductDesc(productDesc);
        return sco;
    }

    public Product validateProductInfos() {
        String productID = "";
        String productName = "";
        ProductCategory productCategory = null;
        String productDesc = "";
        if (creationView != null) {
            productID = creationView.getTxtProductID().getText().trim();
            productName = creationView.getTxtProductName().getText().trim();
            productCategory = (ProductCategory) creationView.getCbxCategories().getSelectedItem();
            productDesc = creationView.getTxaDescription().getText().trim();
        } else if (updateView != null) {
            productID = updateView.getTxtProductID().getText().trim();
            productName = updateView.getTxtProductName().getText().trim();
            productCategory = (ProductCategory) updateView.getCbxCategories().getSelectedItem();
            productDesc = updateView.getTxaDescription().getText().trim();
        }

        if (productID.isEmpty()) {
            MessageDialogUtils.dialogError(null, "Product ID must not be blank!");
            requestFocusProductID();
            return null;
        } else {
            if (productID.length() > 20) {
                MessageDialogUtils.dialogError(null, "Product ID must be maximum of 20 characters!");
                requestFocusProductID();
                return null;
            } 
            if (creationView!=null && productDao.checkIDExist(productID)) {
                MessageDialogUtils.dialogError(null, "Product ID aleady exists! Please choose another ID");
                requestFocusProductID();
                return null;
            }
        }

        if (productCategory.getId()==null) {
            MessageDialogUtils.dialogError(null, "You have to select product category!");
            return null;
        }
        if (productName.isEmpty()) {
            MessageDialogUtils.dialogError(null, "Product Name must not be blank!");
            requestFocusProductName();
            return null;
        } else {
            if (productName.length() > 50) {
                MessageDialogUtils.dialogError(null, "Product Name must be maximum of 50 characters!");
                requestFocusProductName();
                return null;
            }
            if (productDao.checkProductNameExist(productName)) {
                MessageDialogUtils.dialogError(null, "Product Name already exists.Please choose another ID!");
                requestFocusProductName();
                return null;
            }
        }

        if (!productDesc.isEmpty() && productDesc.length() > 50) {
            MessageDialogUtils.dialogError(null, "Product Description must be maximum of 50 characters!");
            requestFocusProductDesc();
            return null;
        }

        return new Product(productID, productName, productDesc, productCategory);
    }

    void requestFocusProductID() {
        if (creationView != null) {
            creationView.getTxtProductID().requestFocus();
        } else if (updateView != null) {
            updateView.getTxtProductID().requestFocus();
        }
    }

    void requestFocusProductName() {
        if (creationView != null) {
            creationView.getTxtProductName().requestFocus();
        } else if (updateView != null) {
            updateView.getTxtProductName().requestFocus();
        }
    }
    
    void requestFocusProductDesc() {
        if (creationView != null) {
            creationView.getTxaDescription().requestFocus();
        } else if (updateView != null) {
            updateView.getTxaDescription().requestFocus();
        }
    }
}